package com.keur.money.ui.user.impl

import android.content.Context
import com.keur.money.R
import com.keur.money.base.utils.log.MullaLog
import com.keur.money.http.ApiService
import com.keur.money.http.HttpHelper
import com.keur.money.http.IHttpCallback
import com.keur.money.mvp.presenter.MvpPresenter
import com.keur.money.ui.user.view.IKeurUserFeedbackView
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/16 16:26
 * @Annotation：意见反馈 Presenter 实现
 */
class KeurUserFeedbackPresenterImpl(context: Context?) : MvpPresenter<IKeurUserFeedbackView>(context) {

    private val typeTitleList = arrayListOf(R.string.feedback_type_suggest,R.string.feedback_type_bug,R.string.feedback_type_repayment_issues,R.string.feedback_type_other)
    private val typeValueList = arrayListOf("SUGGEST","BUG","PAYMENT","OTHER")

    //反馈类型列表
    private var typeList: ArrayList<JSONObject>? = null

    /**
     * 获取意见反馈类型
     */
    fun loadQuestionTypeListAction() {
        if (typeList == null){
            typeList = arrayListOf()
        }
        typeList?.clear()
        for (i in 0..<typeTitleList.size){
            val obj = JSONObject()
            obj.put("title", typeTitleList[i])
            obj.put("value", typeValueList[i])
            obj.put("isSelect",false)
            typeList?.add(obj)
        }
        typeList?.let {
            mvpView?.setSibQuestionTypeList(it)

        }
    }

    /**
     * 图片上传
     */
    fun uploadMullaImageAction(imgPath: String){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.KEUR_API_UPLOAD_FILE_URL)
        HttpHelper.uploadKeurFileAction(httpUrl,imgPath,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                mvpView?.hideMullaLoading()
                when(data.optInt("code")){
                    0 -> {//成功
                        MullaLog.e("image upload success ：$data")
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val imgUrl = it.optString("url")
                            mvpView?.showSibUploadImageSuccess(imgUrl)
                        }
                    }
                    else -> {
                        mvpView?.showMullaToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                MullaLog.e("upload error： $error")
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })

//        HttpHelper.uploadMullaFileAction(httpUrl,imgPath,object : StringCallback() {
//            override fun onSuccess(response: Response<String>?) {
//                if (mvpView == null){
//                    return
//                }
//                when(response?.code()){
//                    200 -> {
//                        val data = JSONObject(response.body())
//                        when(data.optInt("code")){
//                            0 -> {//成功
//                                MullaLog.e("image upload success ：$data")
//                                val imgUrl = data.optJSONObject("obj")?.optString("url")
//                                mvpView?.showSibUploadImageSuccess(imgUrl)
//                            }
//                            else -> {
//                                mvpView.showMullaToast(data.optString("msg"))
//                            }
//                        }
//                    }
//                    else -> {
//                        mvpView.showMullaToast(response.message())
//                    }
//                }
//            }
//            override fun onError(response: Response<String>?) {
//                super.onError(response)
//                if (mvpView != null){
//                    mvpView.showMullaToast(response?.message())
//                }
//            }
//
//            override fun onFinish() {
//                if (mvpView != null){
//                    mvpView.hideMullaLoading()
//                }
//            }
//        })
    }

    /**
     * 提交反馈
     */
    fun submitMullaFeedbackAction(params: JSONObject){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.KEUR_API_FEEDBACK_SUBMIT_PATH)
        HttpHelper.postKeurAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val msg = data.optString("msg")
                mvpView?.showMullaToast(msg)
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView?.showSibSubmitSuccess()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }

    /**
     * 获取全部问题类型列表
     */
    fun getTypeList(): ArrayList<JSONObject>?{
        return typeList
    }
}